package com.spider.pipeline;

import com.spider.entites.Filed;
import com.spider.utils.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 详情页数据执行器
 *
 * @author panglin
 * @date 2020/11/20 21:40
 */
@Component
@Scope("prototype")
public class DetailPagePipeline implements Pipeline {

    private static final Logger LOGGER = LoggerFactory.getLogger(DetailPagePipeline.class);

    /**
     * 字段总数
     */
    private List<Filed> fileds;

    /**
     * 表名
     */
    private String tbName;

    private String id;

    /**
     * 查询工具
     */
    private QueryRunner queryRunner;

    public Pipeline initPipeline(List<Filed> fileds,String tbName,String id,QueryRunner queryRunner){
        this.fileds = fileds;
        this.tbName = tbName;
        this.id = id;
        this.queryRunner = queryRunner;
        return this;
    }

    /**
     * 将爬取的数据写入数据库中
     * 主要分两种情况，一种是两字段，一种是一字段
     *
     * @param resultItems
     * @param task
     */
    @Override
    public void process(ResultItems resultItems, Task task) {
        List<String> cols = new ArrayList<>();
        List<String> datas = new ArrayList<>();
        this.fileds.forEach(filed -> {
            if (StringUtils.isNotEmpty(resultItems.get(filed.getFiled()))) {
                cols.add(filed.getFiled());
                datas.add(resultItems.get(filed.getFiled()));
            }
        });
        if (cols.size()!=0){
            this.finish(cols, datas);
        }
    }

    /**
     * 批量插入
     * @param cols
     * @param datas
     */
    private void finish(List<String> cols,List<String> datas){
        try {
            this.queryRunner.batch(this.generateSql(cols), this.generateParams(cols, datas));
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
    }

    /**
     * 生成预编译参数
     * @param cols
     * @param datas
     * @return
     */
    private Object[][] generateParams(List<String> cols,List<String> datas){
        int size = cols.size();
        Object[][] params = new Object[1][];
        params[0] = new Object[size];
        for (int i = 0; i < size; i++) {
            params[0][i] = datas.get(i);
        }
        return params;
    }

    /**
     * 生成sql
     * @param cols
     * @return
     */
    private String generateSql(List<String> cols) {
        StringBuilder prefix = new StringBuilder("update " + this.tbName + " set ");
        for (String col : cols) {
            prefix.append(col).append("=?,");
        }
        prefix = new StringBuilder(prefix.substring(0, prefix.length() - 1) + " where id=" + this.id);
        LOGGER.info("详情页sql:{}",prefix);
        return prefix.toString();
    }
}
